<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="custName">
        <el-input v-model="queryParams.custName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目编号" prop="itemNo">
        <el-input v-model="queryParams.itemNo" placeholder="请输入项目编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['erp:salecont:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['erp:salecont:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="multiple" @click="handleDisable"
          v-hasPermi="['erp:salecont:edit']">启用</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-edit" size="mini" :disabled="multiple" @click="handleDisable"
          v-hasPermi="['erp:salecont:edit']">禁用</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['erp:salecont:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="salecontList" :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column label="客户名称" align="center" prop="name" width="150" />
      <el-table-column label="甲方" align="center" prop="partyA" width="150" />
      <el-table-column label="签订合同方" align="center" prop="signParty" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_sign_party" :value="scope.row.signParty" />
        </template>
      </el-table-column>
      <el-table-column label="项目编号" width="150" align="center" prop="itemNo" />
      <el-table-column label="订单类别" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_order_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="合同编号" align="center" prop="contractNo" width="150" />
      <el-table-column label="合同金额" align="center" prop="contractAmount" />
      <el-table-column label="E-详设" align="center" prop="edetailDesignAmount" />
      <el-table-column label="税率" align="center" prop="edetailDesignTax" />
      <el-table-column label="E-生设" align="center" prop="eproDesignAmount" />
      <el-table-column label="税率" align="center" prop="eproDesignTax" />
      <el-table-column label="P项" align="center" prop="ptermAmount" />
      <el-table-column label="税率" align="center" prop="ptermTax" />
      <el-table-column label="C项" align="center" prop="ctermAmount" />
      <el-table-column label="税率" align="center" prop="ctermTax" />
      <!-- <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="是否生效" align="center" prop="isEffect" width="180">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isEffect" active-value="" active-text="是" :inactive-value="false" inactive-text="否" @change="effectSalecont(scope.row.id)">
          </el-switch>
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip  />
      <!-- <el-table-column label="附件" align="center" prop="fileUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.fileUrl" :width="50" :height="50" />
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" width="220px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-search" @click="handleView(scope.row)">查看</el-button>
          <!-- <section class="file-preview">
            <el-button size="mini" type="text" icon="el-icon-zoom-in">附件</el-button> -->
            <!-- <el-image ref="preview" :src="scope.row.fileUrl" /> -->
            <!-- <image-preview class="preview" :src="scope.row.fileUrl"
              style="width: 40px;height: 25px;position: absolute;top: 0;left: 0;opacity: 0;z-index: 99;" />
          </section> -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['erp:salecont:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-close" @click="handleDisable(scope.row)"
            v-hasPermi="['erp:salecont:edit']" v-if="scope.row.status === 1">禁用</el-button>
          <el-button size="mini" type="text" icon="el-icon-check" @click="handleDisable(scope.row)"
            v-hasPermi="['erp:salecont:edit']" v-else>启用</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['erp:salecont:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改销售合同清单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="custId">
              <el-select v-model="form.custId" placeholder="请选择客户" filterable :disabled="isReadOnly" @change="getPartyCus($event)">
                <el-option v-for="item in custOptions" :key="item.id" :label="item.name"
                  :value="item.id + ''"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="甲方" prop="partyA">
              <el-input v-model="form.partyA" placeholder="请输入甲方" style="width: 220px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="签订合同方" prop="signParty">
              <el-select v-model="form.signParty" placeholder="请选择签订合同方" :disabled="isReadOnly">
                <el-option v-for="dict in dict.type.erp_sign_party" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目编号" prop="itemNo">
              <el-input v-model="form.itemNo" placeholder="请输入项目编号" style="width: 220px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="itemName">
              <el-input v-model="form.itemName" placeholder="请输入项目名称" style="width: 220px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同编号" prop="contractNo">
              <el-input v-model="form.contractNo" placeholder="请输入合同编号" style="width: 220px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单类别" prop="type">
              <el-select v-model="form.type" placeholder="请选择订单类别" :disabled="isReadOnly">
                <el-option v-for="dict in dict.type.erp_order_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同金额" prop="contractAmount">
              <el-input v-model.number="form.contractAmount" placeholder="请输入合同金额" style="width: 220px;"
                :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.type">
          <el-col :span="12">
            <el-form-item label="E-详设" prop="edetailDesignAmount">
              <el-input v-model.number="form.edetailDesignAmount" placeholder="请输入E-详设金额" style="width: 220px;"
                :disabled="isReadOnly || !form.type.includes('E')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税率" prop="edetailDesignTax">
              <el-input v-model.number="form.edetailDesignTax" placeholder="请输入E-详设税率" style="width: 220px;"
                :disabled="isReadOnly || !form.type.includes('E')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.type">
          <el-col :span="12">
            <el-form-item label="E-生设" prop="eproDesignAmount">
              <el-input v-model.number="form.eproDesignAmount" placeholder="请输入E-生设金额" style="width: 220px;"
                :disabled="isReadOnly || !form.type.includes('E')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税率" prop="eproDesignTax">
              <el-input v-model.number="form.eproDesignTax" placeholder="请输入E-生设税率" style="width: 220px;"
                :disabled="isReadOnly || !form.type.includes('E')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.type">
          <el-col :span="12">
            <el-form-item label="P项" prop="ptermAmount">
              <el-input v-model.number="form.ptermAmount" placeholder="请输入P项金额" style="width: 220px;"
                :disabled="isReadOnly || !form.type.includes('P')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税率" prop="ptermTax">
              <el-input v-model.number="form.ptermTax" placeholder="请输入P项税率" style="width: 220px;"
                :disabled="isReadOnly || !form.type.includes('P')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.type">
          <el-col :span="12">
            <el-form-item label="C项" prop="ctermAmount">
              <el-input v-model.number="form.ctermAmount" placeholder="请输入C项金额" style="width: 220px;"
                :disabled="isReadOnly || !form.type.includes('C')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税率" prop="ctermTax">
              <el-input v-model.number="form.ctermTax" placeholder="请输入P项税率" style="width: 220px;"
                :disabled="isReadOnly || !form.type.includes('C')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="附件" prop="fileUrl">
          <image-upload v-model="form.fileUrl" :disabled="isReadOnly" />
        </el-form-item>
        <el-form-item label="描述说明" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" :disabled="isReadOnly" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!isReadOnly">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSalecont, getSalecont, delSalecont, addSalecont, updateSalecont, disableSalecont, effectSalecont } from "@/api/erp/salecont";

export default {
  name: "Salecont",
  dicts: ['erp_order_type', 'erp_sign_party'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售合同清单表格数据
      salecontList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custName: null,
        itemNo: null,
      },
      // 客户选项
      custOptions: [],
      // 甲方选项
      partyACustOptions: [],
      // 表单参数
      form: {
        type: '1'
      },
      // 表单校验
      rules: {
        custId: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        partyA: [
          { required: true, message: "甲方不能为空", trigger: "blur" }
        ],
        signParty: [
          { required: true, message: "签订合同方不能为空", trigger: "blur" }
        ],
        itemNo: [
          { required: true, message: "项目编号不能为空", trigger: "blur" }
        ],
        contractNo: [
          { required: true, message: "合同编号不能为空", trigger: "blur" }
        ],
        contractAmount: [
          { required: true, message: "合同金额不能为空", trigger: "blur" },
          // { pattern: '^\\d+(\\.\\d+)?$', message: "合同金额必须为数字", trigger: "blur" }
        ],
        type: [
          { required: true, message: "订单类别不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      },
      isReadOnly: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (row.status === 0) {
        return 'warning-row';
      }
      return '';
    },
    /** 查询销售合同清单列表 */
    getList() {
      this.loading = true;
      listSalecont(this.queryParams).then(response => {
        this.salecontList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 是否生效销售合同清单列表 */
    effectSalecont(id) {
      let param = {
        id: id
      }
      effectSalecont(param).then(response => {
        setTimeout(() => {
          this.getList();
        }, 500);
      });
    },
    getPartyCus (value) {
      this.custOptions.forEach(item => {
        if (item.id == value) {
          this.form.partyA = item.name
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        custId: null,
        partyaCustId: null,
        signParty: null,
        itemNo: null,
        contractNo: null,
        contractAmount: null,
        type: null,
        createId: null,
        createTime: null,
        modifyId: null,
        modifyTime: null,
        fileUrl: null,
        isDel: null,
        status: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getSalecont().then(response => {
        this.custOptions = response.custList;
        this.partyACustOptions = response.partyACustList;
        this.open = true;
        this.title = "添加销售合同清单";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.isReadOnly = true
      this.reset();
      const id = row.id || this.ids
      getSalecont(id).then(response => {
        this.custOptions = response.custList;
        this.partyACustOptions = response.partyACustList;
        this.form = response.data;
        this.open = true;
        this.title = "查看销售合同清单";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isReadOnly = false
      this.reset();
      const id = row.id || this.ids
      getSalecont(id).then(response => {
        this.custOptions = response.custList;
        this.partyACustOptions = response.partyACustList;
        this.form = response.data;
        this.open = true;
        this.title = "修改销售合同清单";
      });
    },
    /** 启用禁用按钮操作 */
    handleDisable(row) {
      // this.reset();
      const id = row.id || this.ids
      let param = {
        id: id.toString()
      }
      disableSalecont(param).then(response => {
        console.log('response', response.msg)
        this.$modal.msgSuccess(response.msg);
        this.getList();
        // this.form = response.data;
        // this.open = true;
        // this.title = "修改客户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSalecont(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSalecont(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除销售合同清单编号为"' + ids + '"的数据项？').then(function () {
        return delSalecont(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.id = this.ids.join(",");
      this.download('erp/salecont/export', {
        ...this.queryParams
      }, `销售合同清单_${new Date().getTime()}.xls`)
    },
    /** 打开大图预览 */
    onPreview() {
      this.$nextTick(() => {
        this.$refs.preview.showViewer = true
      })
    }
  }
};
</script>
<style>
.el-table .warning-row {
  background: oldlace;
}

.file-preview {
  display: inline-block;
  position: relative;
  margin: 0 5px;
}
</style>
